HashSet小练习
Employee类有name sal birthday(MyDate[year,moth,day]) 三个属性你 * 如果name+birthday相同判断为统一个对象
注意点
* birthday的不同取决于MyDate的不同,所以要先重写MyDate的hashcode()和equals()方法
package cn.usts.edu.hashSet;
import java.util.HashSet;
import java.util.Objects;
/**
* @author :fly
* @description: Employee类有name sal birthday(MyDate[year,moth,day]) 三个属性你
* 如果name+birthday相同判断为统一个对象
*
* 注意点:
* birthday的不同取决于MyDate的不同,所以要先重写MyDate的hashcode()和equals()方法
*
* @date :2021/11/2 15:24
*/
public class Employee {
private String name;
private float sal;
private MyDate birthday;
public static void main(String[] args) {
= new Employee("jack", 4500.22f, new MyDate(1999, 10, 5));
Employee jack = new Employee("jack", 5500.22f, new MyDate(1999, 10, 5));// sal 不同
Employee jack1 = new Employee("jack", 5500.22f, new MyDate(1999, 10, 6));// day不同
Employee jack2 = new Employee("jack", 5500.22f, new MyDate(1999, 11, 5));// month不同
Employee jack3 = new Employee("jack", 3500.22f, new MyDate(1998, 10, 5));// year不同
Employee jack4
= new Employee("luck", 5500.22f, new MyDate(1999, 10, 5));
Employee luck = new Employee("luck2", 5500.22f, new MyDate(1999, 10, 5));
Employee luck2
HashSet set = new HashSet();
.add(jack);
set.add(jack1);
set.add(jack2);
set.add(jack3);
set.add(jack4);
set.add(luck);
set.add(luck2);
set.forEach(obj-> System.out.println(obj));
set
}
public Employee() {
}
public Employee(String name, float sal, MyDate birthday) {
this.name = name;
this.sal = sal;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSal() {
return sal;
}
public void setSal(float sal) {
this.sal = sal;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", sal=" + sal +
", date=" + birthday +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
= (Employee) o;
Employee employee return Objects.equals(name, employee.name) && Objects.equals(birthday, employee.birthday);
}
@Override
public int hashCode() {
return Objects.hash(name, birthday);
}
}
class MyDate{
private int year;
private int moth;
private int day;
public MyDate(int year, int moth, int day) {
this.year = year;
this.moth = moth;
this.day = day;
}
public MyDate() {
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMoth() {
return moth;
}
public void setMoth(int moth) {
this.moth = moth;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return "MyDate{" +
"year=" + year +
", moth=" + moth +
", day=" + day +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
= (MyDate) o;
MyDate myDate return year == myDate.year && moth == myDate.moth && day == myDate.day;
}
@Override
public int hashCode() {
return Objects.hash(year, moth, day);
}
}